﻿namespace Rds;

public static partial class RdsMethod
{
    #region 对象Kv存取
    public static void ObjkvSave<T>(string dataname, T obj)
    {
        ZKvSet(dataname, obj.Jstr());
    }
    public static bool ObjKvLoad<T>(string dataname, out T obj) where T : new()
    {
        var data = ZKvGet(dataname);
        return J2O(data, out obj);
    }
    #endregion

    #region 对象hash存取
    public static void ObjHashSave<Tk, T>(string dataname, Tk uid, T obj)
    {
        ZHashSet(dataname, uid, obj.Jstr());
    }
    public static bool ObjHashLoad<Tk, T>(string dataname, Tk uid, out T obj) where T : new()
    {
        var data = ZHashGet(dataname, uid);
        return J2O(data, out obj);
    }
    public static void ObjHashRemove<Tk>(string dataname, Tk uid)
    {
        ZHashDelete(dataname, uid);
    }
    #endregion

    #region 基本数据值
    public static void LoadData(this Dictionary<string, string> dict, string key, ref string value)
    {
        if (dict.TryGetValue(key, out var result)) { value = result; }
    }
    public static void LoadData(this Dictionary<string, string> dict, string key, ref int value)
    {
        if (dict.TryGetValue(key, out var result)) { value = result.ToInt(); }
    }
    public static void LoadData(this Dictionary<string, string> dict, string key, ref long value)
    {
        if (dict.TryGetValue(key, out var result)) { value = result.ToLong(); }
    }
    public static void LoadData(this Dictionary<string, string> dict, string key, ref float value)
    {
        if (dict.TryGetValue(key, out var result)) { value = result.ToFloat(); }
    }
    public static void LoadData(this Dictionary<string, string> dict, string key, ref bool value)
    {
        if (dict.TryGetValue(key, out var result)) { value = result.ToBool(); }
    }
    public static void LoadData(this Dictionary<string, string> dict, string key, ref DateTime value)
    {
        if (dict.TryGetValue(key, out var result)) { value = result.ToDateTime(); }
    }
    public static void LoadDataMod<T>(this Dictionary<string, string> dict, string key, ref T value)
    {
        if (dict.TryGetValue(key, out var result)) { try { value = result.Jobj<T>(); } catch { } }
    }
    #endregion

}
